/* $Id: JMapFrame.java 63 2010-02-14 13:52:34Z aaron.day $ */
package org.crazydays.gameplan.map.swing;


import java.awt.Dimension;
import java.awt.Point;
import javax.swing.JInternalFrame;
import javax.swing.JLayeredPane;
import javax.swing.JScrollPane;

import org.crazydays.gameplan.map.GameMap;

/**
 * JMapFrame is the implementation of an JInternalFrame to display a GameMap.
 * The frame consists of a JScrollPane which contains a JMapPane. A JMap and
 * JMapGrid is added to the JMapPane.
 */
public class JMapFrame
    extends JInternalFrame
{
    /** serial version uid */
    public final static long serialVersionUID =
        "$Id: JMapFrame.java 63 2010-02-14 13:52:34Z aaron.day $".hashCode();

    /** game map */
    protected GameMap gameMap;

    /** scroll pane */
    protected JScrollPane scrollPane;

    /** layered pane */
    protected JMapPane mapPane;

    /** map */
    protected JMap map;

    /** map grid */
    protected JMapGrid grid;

    /**
     * JMapFrame constructor.
     * 
     * @param gameMap Game map
     */
    public JMapFrame(GameMap gameMap)
    {
        this(gameMap, new Point(0, 0));
    }

    /**
     * JMapFrame constructor.
     * 
     * @param gameMap Game map
     * @param location Location
     */
    public JMapFrame(GameMap gameMap, Point location)
    {
        super(gameMap.getName(), true, true, true);

        setLocation(location);
        this.gameMap = gameMap;

    }

    /**
     * Initialize map frame.
     */
    public void initialize()
    {
        initializeContentPane();
        initializeMap();
        initializeMapGrid();

        setVisible(true);
        pack();
    }

    /**
     * Initialize content pane as layered pane.
     */
    protected void initializeContentPane()
    {
        mapPane = new JMapPane();
        mapPane.setPreferredSize(getGameMapPreferredSize());
        mapPane.setVisible(true);

        scrollPane =
            new JScrollPane(mapPane, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
                JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

        setContentPane(scrollPane);
    }

    /**
     * Get game map preferred dimension.
     * 
     * @return Dimension
     */
    protected Dimension getGameMapPreferredSize()
    {
        return new Dimension(gameMap.getGeometry().getWidth(), gameMap
            .getGeometry().getHeight());
    }

    /**
     * Initialize map layer.
     */
    protected void initializeMap()
    {
        map = new JMap(gameMap);
        map.setVisible(true);
        mapPane.add(map, JLayeredPane.DEFAULT_LAYER);
    }

    /**
     * Initialize map geometry layer.
     */
    protected void initializeMapGrid()
    {
        grid = new JMapGrid(gameMap.getGeometry());
        grid.setVisible(true);
        mapPane.add(grid, JLayeredPane.DRAG_LAYER - 1);
    }

    /**
     * Set grid visible.
     * 
     * @param visible Visible
     */
    public void setGridVisible(boolean visible)
    {
        grid.setVisible(visible);
    }

    /**
     * Is grid visible.
     * 
     * @return Visible
     */
    public boolean isGridVisible()
    {
        return grid.isVisible();
    }

    /**
     * Get game map.
     * 
     * @return GameMap
     */
    public GameMap getGameMap()
    {
        return gameMap;
    }
}
